# Create your views here.

from django.views import View

from model.models import User, Point
from server.utils import Response, Request, LoginRequiredSuperAdmin, Verify, Code


class ApplyPointView(LoginRequiredSuperAdmin, View):
    """ Application Point """

    @Request.getJson
    def post(self, request):
        """ Approval Application Point """
        pid = request.JSON.get('pid')
        status = request.JSON.get('status')

        code = Verify.pointPid(pid)
        if code != Code.OK:
            return Response.Json(code=code)
        if int(status) not in [Point.Status.apply_approve, Point.Status.apply_not_approve]:
            return Response.Json(code=Code.BadRequest)

        point = Point.objects.get(pid=pid)
        if point.type != Point.Type.person:
            return Response.Json(code=Code.Error, msg='This point is already shared')
        elif point.status != User.Status.applying:
            return Response.Json(code=Code.Error, msg='This point has not applied for sharing')

        point.set_status(status)
        return Response.Json()
